package top.soulblack.purchasesale.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.mapper.UserMapper;
import top.soulblack.purchasesale.mapper.WechatGeneralAccessMapper;
import top.soulblack.purchasesale.model.User;
import top.soulblack.purchasesale.model.WechatTableClassify;
import top.soulblack.purchasesale.service.UserService;
import top.soulblack.purchasesale.model.User;


import javax.naming.AuthenticationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class WechatAccessService {
    @Autowired
    private WechatGeneralAccessMapper wechatGeneralAccessMapper;

    /***
     * @Author li
     * @Description 通过主键筛选
     * @Date 8:46 2019/9/4
     * @Param [table, index, id, columns]【表名，索引名，id，表项】
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> selectByPrimaryKey(String table,String index,Integer id,List<String> columns){
        return wechatGeneralAccessMapper.selectByPrimaryKey(table,index,id,columns);
    }
    /***
     * @Author li
     * @Description 通过主键筛选
     * @Date 9:22 2019/9/4
     * @Param [table, id, columns]  【table实例，id，表项】
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> selectByPrimaryKey(WechatTableClassify table,Integer id,List<String> columns){
        return this.selectByPrimaryKey(table.getName(),table.getIndex(),id,columns);
    }
    /***
     * @Author li
     * @Description 通过主键筛选所有信息
     * @Date 9:22 2019/9/4
     * @Param [table, id]   【table实例，id】
     * @return java.util.Map<java.lang.String,java.lang.Object>
     */
    public Map<String,Object> selectByPrimaryKey(WechatTableClassify table,Integer id){
        return this.selectByPrimaryKey(table,id,new ArrayList<>());
    }

    /***
     * @Author li
     * @Description 筛选数据
     * @Date 9:26 2019/9/4
     * @Param [table表名, columns表项, condiction筛选条件, order顺序]
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    public List<Map<String,Object>> selectList(String table, List<String>columns, List<String>condiction, List<String>order){
        return wechatGeneralAccessMapper.selectList(table,columns,condiction,order);
    }

    public List<Map<String,Object>> selectList(WechatTableClassify table,List<String>columns, List<String>condiction, List<String>order){
        return this.selectList(table.getName(),columns,condiction,order);
    }

    public List<Map<String,Object>> selectList(WechatTableClassify table,List<String>columns, List<String>condiction){
        return this.selectList(table,columns,condiction,new ArrayList<>());
    }
    public List<Map<String,Object>> selectList(WechatTableClassify table,List<String>condiction){
        return  this.selectList(table,new ArrayList<>(),condiction);
    }
    /***
     * @Author li
     * @Description 筛选该表所有数据
     * @Date 9:31 2019/9/4
     * @Param [table]
     * @return java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     */
    public List<Map<String,Object>> selectAll(WechatTableClassify table){
        return  this.selectList(table,new ArrayList<>());
    }

    /***
     * @Author li
     * @Description 通过主键进行更新
     * @Date 9:43 2019/9/4
     * @Param [table, index, id, columns, data]【表名或实例，主键名，id，表项，数据】
     * @return int 成功数量
     */
    public int updateByPrimaryKey(String table, String index, Integer id, List<String>columns, Map<String,Object>data){
        return wechatGeneralAccessMapper.updateByPrimaryKey(table, index, id, columns, data);
    }

    public int updateByPrimaryKey(WechatTableClassify table,Integer id,List<String>columns, Map<String,Object>data){
        return this.updateByPrimaryKey(table.getName(),table.getIndex(),id,columns,data);
    }

    public int updateByPrimaryKey(WechatTableClassify table,Integer id,Map<String,Object>data){
        List<String>columns = new ArrayList<>(data.keySet());
        return this.updateByPrimaryKey(table, id, columns,data);
    }

    public int updateByPrimaryKey(WechatTableClassify table,Map<String,Object>data){
        return this.updateByPrimaryKey(table,(Integer)data.get(table.getIndex()),data);
    }

    /***
     * @Author li
     * @Description 插入新数据
     * @Date 15:03 2019/9/4
     * @Param [table, data] +
     * @return int
     */
    public int insert(String table, Map<String,Object>data){
        return  wechatGeneralAccessMapper.insert(table,data);
    }

    public int insert(WechatTableClassify table,Map<String,Object>data){
        return this.insert(table.getName(),data);
    }



}
